define(['app'],function(app){
    return {module: function($scope, $http, eventBusService, httpService, params, configService) {
	    $scope.form = {};
	    $scope.form.menu_id = params.father_id;
	    
        httpService.post(app.urls.menu_select,{"id" : $scope.form.menu_id}, function(data) {
    		if("0000" === data.code){
    			$scope.menu_name = data.data[0].name;
    		}else{
    			alert(data.msg);
    		}
    	});
        
	    //按钮列表
		httpService.post(app.urls.button_select,{"menu_id" : $scope.form.menu_id}, function(data) {
			if("0000" === data.code){
				$scope.buttonList = data.data;
			}else{
				alert(data.msg);
			}
		});
        
       	//选择学段学科
       	$scope.subjectTabSelected = function(obj) {
       		$scope.form.fk_stage = obj.fk_stage;
       		$scope.form.fk_subject = obj.id;
       		$('#' + $scope.controllerName + ' #subject li a').removeClass("selected");
       		$('#' + $scope.controllerName + ' #subject .subject_'+$scope.form.fk_subject+" a").addClass('selected');

       		changeNavSubject();
       	}

       	//更换学段学科
       	var changeNavSubject = function() {
       		var str = $('#' + $scope.controllerName + ' .stage_'+$scope.form.fk_stage).text() + $('#' + $scope.controllerName + ' .subject_'+$scope.form.fk_subject+" a").text();
       		$('#' + $scope.controllerName + ' #spanSubject').text(str.trim());

       		getGradeList();
       		getVersionList();
       	}
       	
       	//教材
       	var getVersionList = function() {
           	httpService.post(app.urls.version_selectBySubject, {
           		"fk_subject" : $scope.form.fk_subject
           	}, function(data) {
       			$scope.versionList =  data.data;
       		});
       	}

        //年级列表
        var getGradeList = function() {
            httpService.post(app.urls.grade_selectAll, {"fk_stage" : $scope.form.fk_stage}, function (data) {
                if ("0000" === data.code) {
                    $scope.gradeList = data.data;
                } else {
                    alert(data.msg);
                }
            });
        }
        
        $scope.find = function() {
            httpService.postpage(app.urls.textbook_selectAll, $scope.form, function (data) {
                if ("0000" === data.code) {
                    $scope.dataList = data.data;
                } else {
                    alert(data.msg);
                }
            });
        }

        //学科
        $scope.subjectList = [];
        var getSubjectList = function () {
            httpService.post(app.urls.subject_selectAll, {}, function (data) {
                $scope.subjectList = data.data;
                $scope.$apply();
                if ($scope.subjectList.length > 0) {
                    for(var i = 0; i < $scope.subjectList.length; i++) {
                        if($scope.subjectList[i].fk_stage == $scope.form.fk_stage) {
                            $scope.subjectTabSelected($scope.subjectList[i]);
                            break;
                        }
                    }

                    $scope.find();
                }
            });
        }

        //学段
        $scope.stageList = [];
        httpService.post(app.urls.stage_selectAll, {}, function(data) {
            $scope.stageList = data.data;
            if($scope.stageList.length > 0) {
                $scope.form.fk_stage = $scope.stageList[0].id;
            }
            getSubjectList();
        });

        //选中所有
        $scope.checkAll = function() {
            if ($("#allCheck").not(':disabled').prop("checked")) {
                $("#" + $scope.controllerName + " input[name='dataPk']").each(function() {
                    $(this).not(':disabled').prop("checked", false)
                })
            }else {
                $("#" + $scope.controllerName + " input[name='dataPk']").each(function() {
                    $(this).not(':disabled').prop("checked", true)
                })
            }
        }

        //按钮事件
        $scope.operate = function(event) {
        	$scope[event]();
        }
        
        //新增
        $scope.add = function() {
            eventBusService.publish("modal.page", "modules/content/baseinfo/textbook/add?stage="+$scope.form.fk_stage+"&subject="+$scope.form.fk_subject);
        }

        //编辑
        $scope.edit = function() {
            var values = [];
            $('input[name="dataPk"]:checked').each(function() {
                values.push($(this).val());
            });

            if(values.length < 1) {
                eventBusService.publish("modal.alert", {"content":"请选择一条数据。"});
            }else if(values.length > 1) {
                eventBusService.publish("modal.alert", {"content":"不能同时选择多行数据，请选择一条数据。"});
            }else{
                eventBusService.publish("modal.page", "modules/content/baseinfo/textbook/edit?id="+values[0]);
            }
        }

        //删除
        $scope.remove = function() {
            var values = [];
            $('input[name="dataPk"]:checked').each(function(){
                values.push($(this).val());
            });
            if(values.length < 1) {
                eventBusService.publish("modal.alert", {"content":"请选择删除的数据。"});
            }else{
                eventBusService.publish("modal.alert", {"content":"确认删除这"+values.length+"条记录吗？", "isAutoClose":false,
                    "confirm":function() {
                        httpService.post(app.urls.textbook_remove,{"ids":JSON.stringify(values)}, function(data) {
                            if(data.code=="0000"){
                                eventBusService.publish("content.page.reload", data.data);
                            }else{
                                eventBusService.publish("modal.alert", {"content":"删除失败。"});
                            }
                        });
                    }
                });
            }
        }

        $scope.select = function() {
            $scope.page.current = 1;
            $scope.find();
        }

        //接收数据刷新
        eventBusService.subscribe('content.page.reload', function(event,data) {
            $scope.select();
        });

    }
}});